package hjslm.Controller.Background;

import hjslm.Bean.UserBean;
import hjslm.Service.IUserService;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import framework.base.controller.BaseController;

@Controller
@RequestMapping("/backhome")
public class HomeController extends BaseController{

	/**
	 * 登入模块
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	IUserService userService;
	
	@Override
	public void setLogger() {
	}
	
	@RequestMapping("/index")
	public ModelAndView index(HttpSession session){
		if (session.getAttribute("user_session") == null) {
			ModelAndView modelAndView = new ModelAndView("background/home/login01");
			return modelAndView;
		}
		else{
			return new ModelAndView("background/home/index");
		}
	}
	
	@RequestMapping("/login")
	public void login(UserBean bean, HttpSession session, HttpServletResponse response) {
		String retVal = "0";
		if (bean.getUserid() == null || bean.getPassword() == null ){
			//判断用户是否为null
		}
		else if(bean.getPassword().trim() == "" || bean.getUserid().trim() ==""){
			//判断是否为""
		}
		else{
			bean = userService.selectOne(bean);
			if (bean != null) {//验证成功
				//把基本信息存入session中
				bean.setPassword(null);
				session.setAttribute("user_session", bean);//把用户存入session中
				retVal = "1";
			} else {
				//验证失败
			}
		}
		try {
			PrintWriter out = response.getWriter();
			out.print(retVal);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();//打印异常
		}
	}
	
	//退出
	@RequestMapping("/loginout")
	public  ModelAndView loginOut(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView("background/home/login01");
		session.removeAttribute("user_session");
		return modelAndView;
	}
	
	@RequestMapping("/top")
	public ModelAndView top() {
		ModelAndView modelAndView = new ModelAndView("background/home/top");
		return modelAndView;
	}
	
	@RequestMapping("/left")
	public ModelAndView left() {
		ModelAndView modelAndView = new ModelAndView("background/home/left");
		return modelAndView;
	}
	
	@RequestMapping("/right")
	public ModelAndView right() {
		ModelAndView modelAndView = new ModelAndView("background/home/right");
		return modelAndView;
	}

}
